package com.definesys.smecrailtransit.module.SiteDetails.Fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailConstant;
import com.definesys.smecrailtransit.base.SmecBaseFragment;
import com.definesys.smecrailtransit.databinding.FragmentEleRealTimeBinding;
import com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsPresenter;
import com.definesys.smecrailtransit.module.SiteDetails.adapter.SiteDetailsRealTimeAdapter;
import com.definesys.smecrailtransit.module.SiteDetails.bean.ELeStatusBean;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import java.util.ArrayList;
import java.util.HashMap;
import static com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsFragment.SiteSwitch;

/**
 * Created by xupeizuo on 2017/8/21.
 */

public class EleRealTimeFragment extends SmecBaseFragment<SiteDetailsPresenter>{

    private FragmentEleRealTimeBinding fragmentEleRealTimeBinding;
    private SiteDetailsRealTimeAdapter siteDetailsRealTimeAdapter;
    private int stationId=12300;
    public static final String GET_ELE_STATUS_SUCCESS="GET_ELE_STATUS_SUCCESS";
    public static final String GET_ERROR_CONTENT_SUCCESS="GET_ERROR_CONTENT_SUCCESS";
    private static SiteDetailsPresenter siteDetailsPresenter;
    private boolean isFirst=true;
    @Override
    protected SiteDetailsPresenter getPersenter() {
        return siteDetailsPresenter;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentEleRealTimeBinding=DataBindingUtil.inflate(inflater, R.layout.fragment_ele_real_time,container,false);
        _initView();
        return fragmentEleRealTimeBinding.getRoot();
    }

    /**
     * 初始化数据
     */
    private void _initView(){
        siteDetailsRealTimeAdapter=new SiteDetailsRealTimeAdapter(getContext(),mPresenter,0,null);
        fragmentEleRealTimeBinding.realTimeRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        fragmentEleRealTimeBinding.realTimeRecycle.setAdapter(siteDetailsRealTimeAdapter);
    }

    /**
     * 定时请求网络数据
     * @param timeInterval
     */
    private void _quartzGetMsg(int timeInterval){

        fragmentEleRealTimeBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                _quartzGetMsg(1);
                getRealTimeStatus(new HashMap<>());
            }
        },timeInterval == 0 ? 0: 300000);
    }


    /**
     * 网络请求
     * @param map
     */
    public void getRealTimeStatus(HashMap<String, Integer> map) {
        map.put("stationId",stationId);
        mPresenter.getRealTimeStatus(map);
    }

    public static EleRealTimeFragment newInstance(SiteDetailsPresenter presenter) {
        Bundle args = new Bundle();
        EleRealTimeFragment fragment = new EleRealTimeFragment();
        fragment.setArguments(args);
        siteDetailsPresenter=presenter;
        return fragment;
    }

    /**
     * 处理切换数据，请求网络
     * @param hashMap
     */
    private void requestData(HashMap<String,Object> hashMap){
        int position=(Integer)hashMap.get("position");
//        if(position == 0){
//            stationId=hashMap.get("stationId") == null ? 0 :(Integer) hashMap.get("stationId");
//            _quartzGetMsg(0);
//            isFirst=false;
//        }
        stationId=hashMap.get("stationId") == null ? 0 :(Integer) hashMap.get("stationId");
        _quartzGetMsg(0);
        isFirst=false;
    }

    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_ELE_STATUS_SUCCESS)
            }
    )
    public void getELeStatusSuccess(ArrayList<ELeStatusBean> arrayList){
        siteDetailsRealTimeAdapter.setArrayList(arrayList);
    }

    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SiteSwitch)
            }
    )
    public void siteSwitch(HashMap<String,Object> hashMap){
        String ways=(String) hashMap.get("way");
        if(ways !=null && ways.equals("1")){
            if(isFirst){
                requestData(hashMap);
            }
        }else {
            requestData(hashMap);
        }
    }
}
